﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{
    public class PG16:Group
    {


        //具有恒定参数或可变的排放换算因子的体源的数量
        int m_NVL1 = 0;

        public int NVL1
        {
            get { return m_NVL1; }
            set { m_NVL1 = value; }
        }


        //单位
        //1 =        g/s
        //   2 =       kg/hr
        //   3 =       lb/hr
        //   4 =     tons/yr
        //   5 =     Odour Unit * m**3/s  (vol. flux of odour compound)
        //   6 =     Odour Unit * m**3/min
        //   7 =     metric tons/yr

        int m_IVLU = 1;

        public int IVLU
        {
            get { return m_IVLU; }
            set { m_IVLU = value; }
        }

        //16C中提供的具有可变排放速率换算因子的源物种的数量
        int m_NSVL1 = 0;

        public int NSVL1
        {
            get { return m_NSVL1; }
            set { m_NSVL1 = value; }
        }

        //任意排放的源的数量，如果大于1，则需要从文件读取
        int m_NVL2 = 0;

        public int NVL2
        {
            get { return m_NVL2; }
            set { m_NVL2 = value; }
        }


        /// <summary>
        /// 体源排放参数
        /// </summary>
        List<VolumeSInfo> m_VolumeInfo = new List<VolumeSInfo>();

        public List<VolumeSInfo> VolumeInfo
        {
            get { return m_VolumeInfo; }
            set { m_VolumeInfo = value; }
        }


        //是否使用了网格化体源的数据
        //（0=否，1-是，从文件VOLEM.DAT中读取）
        int m_IGRDVL = 0;

        public int IGRDVL
        {
            get { return m_IGRDVL; }
            set { m_IGRDVL = value; }
        }

        //网格化体源文件中的有效排放的高度（M）
        double m_VEFFHT = 0;

        public double VEFFHT
        {
            get { return m_VEFFHT; }
            set { m_VEFFHT = value; }
        }


      


        public override string WriteToGroup()
        {
            string Subgroup = "", TempStr;

            TempStr = StrWriter.GetCLine();
            TempStr += "INPUT GROUPS: 16a, 16b, 16c -- Volume source parameters" + "\r\n";
            Subgroup += TempStr;

            TempStr = StrWriter.GetCLine();
            Subgroup += TempStr;

            TempStr = "! NVL1 = " + NVL1.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! IVLU = " + IVLU.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! NSVL1 = " + NSVL1.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! NVL2 = " + NVL2.ToString() + " !" + "\r\n";
            Subgroup += TempStr;


            Subgroup += "!END!" + "\r\n";


            //进行排序
            List<string> arry = Sort.GetPollutantRightOrder();
            List<VolumeSInfo> arryVolumeSInfo = new List<VolumeSInfo>();
            arryVolumeSInfo.Clear();
            foreach (VolumeSInfo V in VolumeInfo)
            {
                arryVolumeSInfo.Add((VolumeSInfo)V.Clone());
            }
            List<PolluteRatesVar> arryPolluteRatesVar = new List<PolluteRatesVar>();

            for (int i = 0; i < VolumeInfo.Count; i++)
            {
                arryPolluteRatesVar.Clear();
                foreach (string a in arry)
                {
                    for (int j = 0; j < VolumeInfo[i].X.ArryPolluteRatesVar.Count; j++)
                    {

                        if (a == VolumeInfo[i].X.ArryPolluteRatesVar[j].PolluteName)
                        {
                            arryPolluteRatesVar.Add(VolumeInfo[i].X.ArryPolluteRatesVar[j]);
                            break;
                        }
                    }
                }
                for (int k = 0; k < arryVolumeSInfo[i].X.ArryPolluteRatesVar.Count; k++)
                {
                    arryVolumeSInfo[i].X.ArryPolluteRatesVar[k] = (PolluteRatesVar)arryPolluteRatesVar[k].Clone();
                }
            }

            //---------------------------------------


            if (arryVolumeSInfo.Count > 0)
            {
                for (int i = 0; i < arryVolumeSInfo.Count; i++)
                {
                    //if (arryVolumeSInfo[i].SourceVarHouEmiRatBase.IsSourceHour == false)
                    //{
                    //    TempStr = "! SRCNAM =  " + arryVolumeSInfo[i].SRCNAM + "  !" + "\r\n";
                    //    TempStr += "! X =        " + Math.Round(arryVolumeSInfo[i].X.XVOL, 5).ToString() + ",        " + Math.Round(arryVolumeSInfo[i].X.YVOL, 5).ToString() + ",        " + Math.Round(arryVolumeSInfo[i].X.HTVOL, 3).ToString() + ",        " + Math.Round(arryVolumeSInfo[i].X.ELEVOL, 3).ToString() + ",        " + Math.Round(arryVolumeSInfo[i].X.SYVOL, 3).ToString() + ",        " + Math.Round(arryVolumeSInfo[i].X.SZVOL, 3).ToString();
                    //    double A = 0;
                    //    for (int j = 0; j < arryVolumeSInfo[i].X.ArryPolluteRatesVar.Count; j++)
                    //    {
                    //        A = arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].Rate * 1000 / 3600;
                    //        if (arryVolumeSInfo[i].X.Unit == "g/s")
                    //        {
                    //            //每隔3行数据就换行
                    //            if (j % 3 == 0)
                    //            {
                    //                TempStr += ",  " + "\r\n" + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].Rate.ToString("E5");
                    //            }
                    //            else
                    //            {
                    //                TempStr += ",  " + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].Rate.ToString("E5");
                    //            }
                    //        }
                    //        else
                    //        {
                    //            if (j % 3 == 0)
                    //            {
                    //                TempStr += ",  " + "\r\n" + A.ToString("E5");
                    //            }
                    //            else
                    //            {
                    //                TempStr += ",  " + A.ToString("E5");
                    //            }
                    //        }
                    //    }

                    //    TempStr += "  !" + "\r\n" + "   ! END !" + "\r\n";
                    //    Subgroup += TempStr;

                    //}




                }

            }

            string F = "";
            for (int i = 0; i < arryVolumeSInfo.Count; i++)
            {

                //if (arryVolumeSInfo[i].SourceVarHouEmiRatBase.IsSourceHour == false)
                //{
                //    string D = "";
                //    for (int j = 0; j < arryVolumeSInfo[i].X.ArryPolluteRatesVar.Count; j++)
                //    {
                //        if (arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].IVARY != 0)
                //        {
                //            string A = "! SRCNAM = " + arryVolumeSInfo[i].SRCNAM + " !" + "\r\n";
                //            string B = "! IVARY  =  " + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].IVARY + "  !" + "\r\n";
                //            string C = "";
                //            for (int k = 0; k < arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient.Count; k++)
                //            {
                //                //C = "";
                //                if (k != arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient.Count - 1)
                //                {
                //                    C = C + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient[k].ToString() + ",";
                //                }
                //                else
                //                {
                //                    C = C + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient[k].ToString() + "      !";
                //                }
                //                if ((k + 1) % 6 == 0)
                //                {
                //                    C = C + "\r\n";
                //                }
                //            }
                //            C = "! " + arryVolumeSInfo[i].X.ArryPolluteRatesVar[j].PolluteName + "          = " + C + "!END!" + "\r\n";
                //            D = D + A + B + C;
                //        }
                //    }
                //    F = F + D;
                //}


            }
            Subgroup = Subgroup + F;

            return Subgroup;
        }

        public override void ReadFromGroup(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> m_dic in dic)
            {
                if (m_dic.Key == "NVL1")
                {
                    this.NVL1 = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "IVLU")
                {
                    this.IVLU = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NSVL1")
                {
                    this.NSVL1 = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NVL2")
                {
                    this.NVL2 = Convert.ToInt32(m_dic.Value);
                }


                VolumeSInfo ps = new VolumeSInfo();
                List<double> lst = new List<double>();

                if (m_dic.Key == "SRCNAM")
                {
                    ps.SRCNAM = m_dic.Value;
                }
                if (m_dic.Key == "X")
                {
                    lst = StrWriter.ReturnDouList(m_dic.Value);
                    ps.X.XVOL = lst[0];
                    ps.X.YVOL = lst[1];
                    ps.X.HTVOL = lst[2];

                    ps.X.ELEVOL = lst[3];
                    ps.X.SYVOL = lst[4];
                    ps.X.SZVOL = lst[5];


                    ps.X.ArryPolluteRatesVar.Clear();
                    for (int i = 6; i < lst.Count; i++)
                    {
                        PolluteRatesVar mPolluteRatesVar = new PolluteRatesVar();
                        mPolluteRatesVar.Rate = lst[i];
                        ps.X.ArryPolluteRatesVar.Add(mPolluteRatesVar);
                    }

                }

                VolumeInfo.Add(ps);


            }

        }



    }



     /// <summary>
   /// 体源排放参数
   /// </summary>
    [Serializable]
    public class VolumeSInfo :  ICloneable
    {
       
        /// <summary>
        /// 污染源名称
        /// </summary>
        string m_SRCNAM = "V";

        public string SRCNAM
        {
            get { return m_SRCNAM; }
            set { m_SRCNAM = value; }
        }
        private int m_Sid = 0;
        /// <summary>
        ///　污染源编号记录器，只增加，不减少以避免重复，且唯一.
        /// </summary>
        public int Sid
        {
            get { return m_Sid; }
            set { m_Sid = value; }
        }


        private string m_Name = "L";
        /// <summary>
        /// 污染源名称，非Inp文件
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }


        /// <summary>
        /// 体源位置信息
        /// </summary>
        VolumeXInfo m_X = new VolumeXInfo();

        public VolumeXInfo X
        {
            get { return m_X; }
            set { m_X = value; }
        }


        //SourceVarHouEmiRatBase m_SourceVarHouEmiRatBase = new SourceVarHouEmiRatBase();
        ///// <summary>
        ///// 小时排放速率参数类
        ///// </summary>
        ///// <returns></returns>
        //public SourceVarHouEmiRatBase SourceVarHouEmiRatBase
        //{
        //    get { return m_SourceVarHouEmiRatBase; }
        //    set { m_SourceVarHouEmiRatBase = value; }
        //}



        //体源排放的每种污染物的排放速率（g/s）
        //double m_QVOL = 0;

        //public double QVOL
        //{
        //    get { return m_QVOL; }
        //    set { m_QVOL = value; }
        //}

        public object Clone()
        {
            VolumeSInfo obj = new VolumeSInfo();
            obj = (VolumeSInfo)this.MemberwiseClone();


            VolumeXInfo mVolumeXInfo = new VolumeXInfo();                   
            mVolumeXInfo =(VolumeXInfo) this.X.Clone();
            
            obj.X = mVolumeXInfo;

            //SourceVarHouEmiRatBase mSourceVarHouEmiRatBase = new SourceVarHouEmiRatBase();
            //mSourceVarHouEmiRatBase = (SourceVarHouEmiRatBase)this.SourceVarHouEmiRatBase.Clone();
            //obj.SourceVarHouEmiRatBase = mSourceVarHouEmiRatBase;


            return obj;
        }
    }
    /// <summary>
    /// 体源位置信息
    /// </summary>
    [Serializable ]
   public class VolumeXInfo:ICloneable 
   {
        /// <summary>
        /// 体源中心X坐标，km
        /// </summary>
       double m_XVOL = 0;
       /// <summary>
       /// 体源中心X坐标，km
       /// </summary>
       public double XVOL
       {
           get { return m_XVOL; }
           set { m_XVOL = value; }
       }
        /// <summary>
       /// 体源中心Y坐标，km
        /// </summary>
       double m_YVOL = 0;
       /// <summary>
       /// 体源中心Y坐标，km
       /// </summary>
       public double YVOL
       {
           get { return m_YVOL; }
           set { m_YVOL = value; }
       }
        /// <summary>
        /// 体源的有效高度
        /// </summary>
       double m_HTVOL = 0;
       /// <summary>
       /// 体源的有效高度
       /// </summary>
       public double HTVOL
       {
           get { return m_HTVOL; }
           set { m_HTVOL = value; }
       }
        /// <summary>
        /// 体源基准海拔高度
        /// </summary>
       double m_ELEVOL = 0;
       /// <summary>
       /// 体源基准海拔高度
       /// </summary>
       public double ELEVOL
       {
           get { return m_ELEVOL; }
           set { m_ELEVOL = value; }
       }
        /// <summary>
        /// 体源初始σy m
        /// </summary>
       double m_SYVOL = 0;
       /// <summary>
       /// 体源初始σy m
       /// </summary>
       public double SYVOL
       {
           get { return m_SYVOL; }
           set { m_SYVOL = value; }
       }
       /// <summary>
       /// 体源初始σz m
       /// </summary>
       double m_SZVOL = 0;
       /// <summary>
       /// 体源初始σz m
       /// </summary>
       public double SZVOL
       {
           get { return m_SZVOL; }
           set { m_SZVOL = value; }
       }
        /// <summary>
        /// 边长
        /// </summary>
       private double m_Length = 0;
        /// <summary>
        /// 边长m
        /// </summary>
       public double Length
       {
           get { return m_Length; }
           set { m_Length = value; }
       }

       private List<PolluteRatesVar> m_arryPolluteRatesVar = new List<PolluteRatesVar>();
       /// <summary>
       /// 污染物速率相关的一此参数
       /// </summary>
       public  List<PolluteRatesVar> ArryPolluteRatesVar
       {
           get { return m_arryPolluteRatesVar; }
           set { m_arryPolluteRatesVar = value; }
       } 
    
       private string m_Unit = "g/s";//单位
       /// <summary>
       /// 单位
       /// </summary>
       public string Unit
       {
           get { return m_Unit; }
           set { m_Unit = value; }
       }
    
       public object Clone()
       {
           VolumeXInfo obj = new VolumeXInfo();
           obj =(VolumeXInfo) this.MemberwiseClone(); 
           return obj;
       }
   }


    [Serializable]
    public class PolluteRatesVar : ICloneable
    {


        private string m_PolluteName = ""; //污染物名称        
        /// <summary>
        /// 污染物名称
        /// </summary>
        public string PolluteName
        {
            get { return m_PolluteName; }
            set { m_PolluteName = value; }
        }
        private double m_Rate = 0;//污染物排放速率     
        /// <summary>
        /// 污染物排放速率
        /// </summary>
        public double Rate
        {
            get { return m_Rate; }
            set { m_Rate = value; }
        }
        private int m_IVARY = 0; //污染源的变化类型
        /// <summary>
        /// 污染源的变化类型
        /// </summary>
        public int IVARY
        {
            get { return m_IVARY; }
            set { m_IVARY = value; }
        }

        private List<double> m_arryCoefficient = new List<double>(); //污染物排放系数
        /// <summary>
        /// 污染物排放系数
        /// </summary>
        public List<double> ArryCoefficient
        {
            get { return m_arryCoefficient; }
            set { m_arryCoefficient = value; }
        }
        public object Clone()
        {
            PolluteRatesVar obj = new PolluteRatesVar();
            obj = (PolluteRatesVar)this.MemberwiseClone();
            List<double> arr = new List<double>();
            for (int i = 0; i < ArryCoefficient.Count; i++)
            {
                arr.Add(this.ArryCoefficient[i]);
            }
            obj.ArryCoefficient = arr;
            return obj;
        }
    }


}




